package com.aabte.design2.structural.flyweight;

import java.util.Collection;
import java.util.LinkedList;

/**
 * 享元工厂决定是否复用已有享元或者创建一个新的对象。
 *
 * @author Daniel
 * @version 1.0
 * @date 2020/5/3
 */
public class TreeFactory {

    private static Collection<TreeType> treeTypes = new LinkedList<>();

    public static TreeType getTreeType(String name, String color, String textTrue) {
        TreeType treeType = findTreeType(name, color, textTrue);
        if (null == treeType) {
            treeType = new TreeType(name, color, textTrue);
            treeTypes.add(treeType);
        }
        return treeType;
    }

    private static TreeType findTreeType(String name, String color, String textTrue) {
        for (TreeType treeType : treeTypes) {
            if (treeType.getName().equals(name) && treeType.getColor().equals(color) && treeType.getTexture().equals(textTrue)) {
                return treeType;
            }
        }
        return null;
    }

}
